﻿package main
{
	import com.adobe.fiber.core.model_internal;
	
	import main.PokerEngine;
	import mx.controls.Alert;
	import mx.collections.ArrayCollection;
	import mx.controls.Label;
	import mx.core.Application;

	[Bindable]
	[RemoteClass(alias="engine.RemoteTable")]
	/**
	 * Klasse die den empfangenen Tisch repraesentiert und Methoden um diese Werte des Tisches anzuzeigen
	 * @author 3KP
	 * 
	 */	
	public class RemoteTable {
		
		public static var instanceOf:RemoteTable;
		public var activePlayerIndex:int;
		public var highestBet:int;
		public var secondHighestBet:int;
		
		public var flop1:String; 
		public var flop2:String;
		public var flop3:String;
		public var turn:String;
		public var river:String;
		
		
		
		public var playersHandDescriptions:Array;
		public var playersPocket1s:Array;
		public var playersPocket2s:Array;
		public var playersNames:Array;
		
		public var playersIsBroke:Array;
		public var playersHasFolded:Array;
		
		public var playersStacks:Array;
		public var playersBets:Array; 
		
		public var mainPot:int;
		public var sidePot1:int;
		public var sidePot2:int;
		public var sidePot3:int;
	
		public var dealerPos:int;
		public var smallBlindPos:int;
		public var bigBlindPos:int;
		public var smallBlind:int;
		public var bigBlind:int;
		
		public var showdown:Boolean;
		public var replay:Boolean;
		
		public function RemoteTable() {super();}
		/**
		 * Weist die tablewerte zu 
		 * @param table
		 * 
		 */
		public static function  drawTable(table:RemoteTable) :void {
			var app : Object = Application.application;

			
			app.pokerEngine.name1.text = table.playersNames[0];
			app.pokerEngine.name2.text = table.playersNames[1];
			app.pokerEngine.name3.text = table.playersNames[2];
			app.pokerEngine.name4.text = table.playersNames[3];
			app.pokerEngine.mainpot.text = "Pot: "+checkValue(table.mainPot);
			app.pokerEngine.sidepot1.text = checkValue(table.sidePot1);
			app.pokerEngine.sidepot2.text = checkValue(table.sidePot2);
			app.pokerEngine.sidepot3.text = checkValue(table.sidePot3);
			app.pokerEngine.stack1.text = checkValue(table.playersStacks[0]);
			app.pokerEngine.stack2.text = checkValue(table.playersStacks[1]);
			app.pokerEngine.stack3.text = checkValue(table.playersStacks[2]);
			app.pokerEngine.stack4.text = checkValue(table.playersStacks[3]);
			app.pokerEngine.bet1.text = checkValue(table.playersBets[0]);
			app.pokerEngine.bet2.text = checkValue(table.playersBets[1]);
			app.pokerEngine.bet3.text = checkValue(table.playersBets[2]);
			app.pokerEngine.bet4.text = checkValue(table.playersBets[3]);
			drawPockets(table);
			help(table);
			app.pokerEngine.flop1.source = checkCard(table.flop1)
			app.pokerEngine.flop2.source = checkCard(table.flop3)
			app.pokerEngine.flop3.source = checkCard(table.flop2)
			app.pokerEngine.turn.source = checkCard(table.turn)
			app.pokerEngine.river.source = checkCard(table.river)
			
			// Position des Dealer Buttons
			
			switch(table.dealerPos){
				case 0: 
					app.pokerEngine.dealerButton.x="130"
					app.pokerEngine.dealerButton.y="210"
					break;
				case 1: 
					app.pokerEngine.dealerButton.x="620"
					app.pokerEngine.dealerButton.y="210"
					break;
				case 2: 
					app.pokerEngine.dealerButton.x="620"
					app.pokerEngine.dealerButton.y="325"
					break;
				case 3: 
					app.pokerEngine.dealerButton.x="130"
					app.pokerEngine.dealerButton.y="325"
					break;
			}
			
			// Farbe der Namensschilder
			
			switch(table.activePlayerIndex){
				case -1:
					app.pokerEngine.namefield1.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield2.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield3.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield4.source="main/img/NamensschildGrau2.png";
					break;
				case 0 :
					app.pokerEngine.namefield1.source="main/img/NamensschildSchwarz2.png";
					app.pokerEngine.namefield2.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield3.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield4.source="main/img/NamensschildGrau2.png";
					break;
				
				case 1 :
					app.pokerEngine.namefield1.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield2.source="main/img/NamensschildSchwarz2.png";
					app.pokerEngine.namefield3.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield4.source="main/img/NamensschildGrau2.png";	
					break;
				
				case 2 :
					app.pokerEngine.namefield1.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield2.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield3.source="main/img/NamensschildSchwarz2.png";
					app.pokerEngine.namefield4.source="main/img/NamensschildGrau2.png";	
					break;
				
				case 3 :
					app.pokerEngine.namefield1.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield2.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield3.source="main/img/NamensschildGrau2.png";
					app.pokerEngine.namefield4.source="main/img/NamensschildSchwarz2.png";
					break;
				
			}
			
			// Aktivierung der Buttons (check,bet,fold oder replay Buttons)
			
			if(GameControl.getUser().name == (table.playersNames[table.activePlayerIndex]) && !table.replay){ 
				// Ueberprueft ob Spieler an der Reihe und ob kein Replay
				// Zeigt Game Buttons und spielt Sound ab
				app.pokerEngine.gameButtons.enabled=true;
				app.pokerEngine.gameButtons.visible=true;
				app.pokerEngine.replayButtons.visible=false;
				app.pokerEngine.betInput.text=Math.min(table.highestBet-table.playersBets[table.activePlayerIndex],table.playersStacks[table.activePlayerIndex]);
				if(table.highestBet == table.playersBets[table.activePlayerIndex]){
					app.pokerEngine.buttonFold.enabled=false;
				} else { 
					app.pokerEngine.buttonFold.enabled=true;
				}
				GameControl.playSound("bing.mp3"); 
				
			} else if(!table.replay){
				// Spieler nicht an der Reihe und kein Replay
				// Zeigt Game Buttons aber disabled
				app.pokerEngine.gameButtons.enabled=false;
				app.pokerEngine.gameButtons.visible=true;
				app.pokerEngine.replayButtons.visible=false;
				app.pokerEngine.betInput.text="";
			} else {
				// Wenn Replay
				// Zeigt Replay Buttons
				app.pokerEngine.gameButtons.enabled=false;
				app.pokerEngine.gameButtons.visible=false;
				app.pokerEngine.replayButtons.visible=true;
				app.pokerEngine.betInput.text="";
			}
			
		}

		/**
		 * Zeigt in der InfoBox die aktuelle Hand des Spielers
		 * @param table
		 * 
		 */		
		public static function help(table:RemoteTable):void{
			var app : Object = Application.application;
			app.pokerEngine.infoBox.text="Ihr aktuelles Blatt:\n"+table.playersHandDescriptions[GameControl.seatPos];
			
		}
		/**
		 * Gibt von einer Karte die dazugehörige Bilddatei zurück 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function checkCard(value:String):String{
			if(value==null){
				return null;
			} else {
				return "main/img/"+value;	
			}
		}
		/**
		 * Zeichnet die Karten 
		 * @param table
		 * 
		 */		
		public static function drawPockets(table:RemoteTable):void{
	 		var app : Object = Application.application;
			var name : String = GameControl.getUser().name;
			//Karten der Positon 1
			if(name == table.playersNames[0] || table.showdown || name == "Adrian"){
				// Wenn Spieler an dieser Position zeige Karten
				app.pokerEngine.p1c1.source = checkCard(table.playersPocket1s[0]);
				app.pokerEngine.p1c2.source = checkCard(table.playersPocket2s[0]);
			} else if(table.playersPocket1s[0]!=null){
				// Wenn Spieler nicht an dieser Position zeige Rückseite
				app.pokerEngine.p1c1.source = "main/img/Kartendeck.png";
				app.pokerEngine.p1c2.source = "main/img/Kartendeck.png";
			} else {
				// Wenn an dieser Postion kein Spieler zeige nix
				app.pokerEngine.p1c1.source = null;
				app.pokerEngine.p1c2.source = null;
			}
			//Karten der Positon 2
			if(name == table.playersNames[1] || table.showdown || name == "Adrian"){
				// Wenn Spieler an dieser Position zeige Karten
				app.pokerEngine.p2c1.source = checkCard(table.playersPocket1s[1]);
				app.pokerEngine.p2c2.source = checkCard(table.playersPocket2s[1]);
			} else if(table.playersPocket1s[1]!=null){
				// Wenn Spieler nicht an dieser Position zeige Rückseite
				app.pokerEngine.p2c1.source = "main/img/Kartendeck.png";
				app.pokerEngine.p2c2.source = "main/img/Kartendeck.png";
			} else {
				// Wenn an dieser Postion kein Spieler zeige nix
				app.pokerEngine.p2c1.source = null;
				app.pokerEngine.p2c2.source = null;
			}
			//Karten der Positon 3
			if(name == table.playersNames[2] || table.showdown || name == "Adrian"){
				// Wenn Spieler an dieser Position zeige Karten
				app.pokerEngine.p3c1.source = checkCard(table.playersPocket1s[2]);
				app.pokerEngine.p3c2.source = checkCard(table.playersPocket2s[2]);
			} else if(table.playersPocket1s[2]!=null){
				// Wenn Spieler nicht an dieser Position zeige Rückseite
				app.pokerEngine.p3c1.source = "main/img/Kartendeck.png";
				app.pokerEngine.p3c2.source = "main/img/Kartendeck.png";
			} else {
				// Wenn an dieser Postion kein Spieler zeige nix
				app.pokerEngine.p3c1.source = null;
				app.pokerEngine.p3c2.source = null;
			}
			//Karten der Positon 4
			if(name == table.playersNames[3] || table.showdown || name == "Adrian"){
				// Wenn Spieler an dieser Position zeige Karten
				app.pokerEngine.p4c1.source = checkCard(table.playersPocket1s[3]);
				app.pokerEngine.p4c2.source = checkCard(table.playersPocket2s[3]);
			} else if(table.playersPocket1s[3]!=null){
				// Wenn Spieler nicht an dieser Position zeige Rückseite
				app.pokerEngine.p4c1.source = "main/img/Kartendeck.png";
				app.pokerEngine.p4c2.source = "main/img/Kartendeck.png";
			} else {
				// Wenn an dieser Postion kein Spieler zeige nix
				app.pokerEngine.p4c1.source = null;
				app.pokerEngine.p4c2.source = null;
			}
		}
		
		/**
		 * Wenn ein int Wert 0 ist wird ein leerer String zurückgegeben sonst die Zahl als String
		 * @param value
		 * @return 
		 * 
		 */		
		public static function checkValue(value:int):String{
			if(value==0) {
				return "";
			} else {
				return ""+value;
			}
		}
		
		/**
		 *  
		 * @return 
		 * 
		 */		
		public static function getInstance():RemoteTable{
			return instanceOf;
		}
	}
}